Skip to content

perf(web): pre-parse selectors once at bucket build; -39% sel_match - #905

Merged
ormastes merged 1 commit into
mainfrom
work/web-perf-round9-2026-09-13
Sep 13, 2026
Merged

ormastes merged 1 commit into
mainfrom
work/web-perf-round9-2026-09-13

Conversation

@ormastes

Copy link
Copy Markdown
Owner

Web-rendering perf round 9, Target A: the structural fix rounds 5-8 identified but did not implement. Three prior rounds on sel_match ended in honest negatives; this one lands with 12 A/B pairs.

What was re-parsed per node

simple_match / simple_match_ctx ran ~4,078 times per 8-page bench run, and every call re-derived from selector TEXT things that do not depend on the node: the base/pseudo split, the :: probe, the [...] bracket walk plus attr_selector_matches' ^=/$=/= probing, flag strip and unquote, class_has_all's per-node .a.b.c split, the pseudo-class chain walk twice (once in simple_match, again in _pseudo_ctx_matches), the :not/:is/:where/:has comma splits, and _nth_child_matches' an+b parse. In the interpreter that is per-character sel[i:i+1] slicing — which round 8 named as the likely dominant cost without sizing it.

The change

Each selector part is parsed once into a ParsedSel record where build_rule_buckets runs, stored on RuleBuckets.parsed_groups (one construction site, unlike Rules), indexed exactly like rules.group_parts. The per-node path — simple_match_parsed, _pseudo_ctx_matches_parsed, simple_match_ctx_parsed, selector_group_matches_node_parsed — consumes only the record and never touches selector text. One pre-parsed chain feeds both pseudo passes, so round 8's duplicate chain walk disappears with it. The _wsc_armed() wrapper shape is preserved so sel_match_ms measures the same span either side.

Equivalence — and the three divergences it caught

New oracle simple_web_layout_debug_selector_parse_equivalence(html, max_nodes) runs both paths over every distinct selector part × every node (base and full-context match) and every real candidate rule-group, pinned by test/01_unit/browser_engine/web_selector_parsed_equivalence_spec.spl (byte-identical twin at test/unit/): 10/10 pass, 177,949 catalog comparisons over 3,419 nodes + an adversarial fixture, 0 mismatches.

Two tiers, because the eight catalog stylesheets contain only 25 distinct selectors between them — catalog-only coverage proves almost nothing about selector semantics. The adversarial half found three real divergences, each added as a fixture row, confirmed failing, then fixed:

  1. :disabled never matches in the text path, on either branch. Without the attribute the _is_interaction_state_pseudo arm answers true and returns false; with it, that arm answers false and the chain falls through to the catch-all arm, which also returns false. The first cut implemented the apparent intent and made it match — a behaviour change, reverted. The quirk is pre-existing at origin/main, documented in _pseudo_code_for, and changing it is a separate decision.
  2. A failed :has() split is a pass-through, not a rejection. simple_match's elif-chain excludes has, so simple_match("ul:has()", <ul>) returns true and only _pseudo_ctx_matches rejects. Invisible on a rightmost part, decisive on an ancestor part (ul:has() > li, ul:has(a,,b) span, a 17-option :has), which runs simple_match only.
  3. A functional argument on :root is parsed then ignored. :root(x) takes the ( branch and the root arm checks only tag != "html"; only :root[...] carries an attribute test. The field is now pseudo_root_attrs, written solely by the [ branch.

None of these three shapes occurs in the catalog, so the Draw IR digests never moved for any of them — which is exactly why the adversarial fixture exists.

Measurements

12 alternating A/B pairs, file swapped in place between runs, SIMPLE_WEB_STYLE_COUNTERS=1.

  • Quiet-host sweep: sel_match 1009-1016 ms → 613-638 ms (−39%) with controls cas_tail (532-540 → 533-569) and sel_sort (92-94 → 93-96) flat.
  • Across all 12 pairs, normalised by cas_tail as the load proxy: before 1.83-1.99, after 1.09-1.16 — non-overlapping and non-adjacent.
  • candidates=4024 and selector_calls=4078 on all 24 runs: the candidate set is provably unchanged, so the delta is match cost, not pruning.
  • sec_select_ms 1360-1369 → 966-1007: the saving flows through, not sideways.
gate verdict
Draw IR sha256, 8/8 pages byte-identical across all 24 runs, equal to round 8's recorded set
web_selector_parsed_equivalence_spec 10/10 pass, 0 mismatches
GPU boundary audit PASS — 2 frame(s) audited, host_pixel_iterations=0, readbacks_per_frame<=1, submits_per_frame<=1
26 selector/style/cascade/inherit specs + web_cold_pipeline_memo_spec verdict lines byte-identical before vs after (19 OK, 7 pre-existing ERROR)

Withdrawn: per-page cold pipeline totals. The ~400 ms saving is below this host's run-to-run swing on a 3-6 s page and the sign flips between pairs on the same page; they are recorded in the metrics doc and explicitly not used as evidence. The load-robust claim is the control-normalised sel_match ratio.

Pre-push guards (rc read into a variable on the next line, never through a pipe): conflict-markers rc=0, tree-size rc=0 (selftest 24/24), no-revert rc=0 (selftest 3/3), hook-wiring rc=0 (12 checks), divergence-delta rc=0 — PASS — 3213 pre-existing offender(s), 0 introduced by this range. The pre-existing red is identical at base and tip; the offender list and base verdict are recorded in the commit message as .claude/rules/vcs.md requires.

Target B (cas_tail, ~535 ms) deliberately untouched — it was the control that made this measurement trustworthy. Next round's target, localised in the doc: css-layout (184 ms) and css-paint (238 ms) carry 79% of it.

Record: doc/10_metrics/ui/web_perf_round9_2026-09-13.md

🤖 Generated with Claude Code

https://claude.ai/code/session_01TVraTPgGDVypESTsVqXPgi

`simple_match` / `simple_match_ctx` re-derived, from selector TEXT, on EVERY
candidate x node pair (~4,078 per 8-page bench run), information that does not
depend on the node at all: the base/pseudo split, the `::` probe, the `[...]`
bracket-group walk plus `attr_selector_matches`' `^=`/`$=`/`=` probing, flag
strip and unquote, `class_has_all`'s per-node `.a.b.c` split, the pseudo-class
chain walk (TWICE -- once in `simple_match`, again in `_pseudo_ctx_matches`),
the `:not/:is/:where/:has` comma splits, and `_nth_child_matches`' `an+b`
parse. In the interpreter that is per-character `sel[i:i+1]` slicing, which
round 8 named as the likely dominant cost without sizing it.

Each selector part is now parsed ONCE into a `ParsedSel` record where
`build_rule_buckets` runs, stored on `RuleBuckets.parsed_groups` (one
construction site, unlike `Rules`), indexed exactly like `rules.group_parts`.
The per-node path -- `simple_match_parsed`, `_pseudo_ctx_matches_parsed`,
`simple_match_ctx_parsed`, `selector_group_matches_node_parsed` -- consumes
only the record and never touches selector text. One pre-parsed pseudo chain
feeds both halves, so round 8's duplicate chain walk disappears with it.
The `_wsc_armed()` wrapper shape is preserved so `sel_match_ms` measures the
same span either side.

Semantics are byte-identical by construction: the parser mirrors
`simple_match`'s check order and reuses the same helpers, and `dead` is set
only where the text path returns false regardless of the node. `check_tag` /
`check_id` are explicit flags, not `""` sentinels, because `#` -> `id == ""`
really does mean "must have no id".

New equivalence oracle
`simple_web_layout_debug_selector_parse_equivalence(html, max_nodes)` runs BOTH
paths over every distinct selector part x every node (base AND full-context
match) and every real candidate rule-group, pinned by
test/01_unit/browser_engine/web_selector_parsed_equivalence_spec.spl (twin at
test/unit/): 10/10 pass, 177,949 comparisons over the 8 catalog pages plus an
adversarial fixture, 0 mismatches.

The adversarial half earned its keep on the first run:
`MISMATCH base node=27 sel=input:disabled text=false parsed=true`. `:disabled`
never matches in the text path on EITHER branch -- without the attribute the
`_is_interaction_state_pseudo` arm answers true and returns false; with it that
arm answers false and the chain falls through to the catch-all arm, which also
returns false. The first cut implemented the apparent intent and was reverted:
`:disabled` now joins the dead set, reproducing the renderer exactly. The quirk
is pre-existing at origin/main and documented in `_pseudo_code_for`; changing
it is a separate decision, not a side effect of a perf round. The catalog pages
would never have caught this -- they contain 25 distinct selectors and no
`:disabled`.

Evidence (doc/10_metrics/ui/web_perf_round9_2026-09-13.md):
- 12 alternating A/B pairs. Quiet-host sweep: sel_match 1009-1016 ms ->
  613-638 ms (-39%) with cas_tail and sel_sort flat. Across all 12 pairs,
  normalised by cas_tail as the load proxy, before 1.83-1.99 vs after
  1.09-1.16 -- non-overlapping, non-adjacent.
- candidates=4024 and selector_calls=4078 on all 24 runs: the candidate set is
  provably unchanged, so the delta is match cost, not pruning.
- Draw IR sha256 byte-identical on all 8 pages across all 24 runs, equal to the
  set round 8 recorded.
- GPU boundary audit: PASS, 2 frames, host_pixel_iterations=0.
- 26 selector/style/cascade/inherit specs + web_cold_pipeline_memo_spec:
  verdict lines byte-identical before vs after (19 OK, 7 pre-existing ERROR).
- Per-page cold pipeline totals are reported but WITHDRAWN as evidence: the
  ~400 ms saving is below this host's run-to-run swing on a 3-6 s page and the
  sign flips between pairs.

Target B (cas_tail, ~535 ms) deliberately untouched -- it was the control that
made this measurement trustworthy. Residuals named in the record: `:not/:is/
:where/:has` options are pre-split but not recursively pre-parsed, and the
`:root[attr]` expression stays text.

Pre-push guards, rc read into a variable on the line after each invocation
(never through a pipe):
- check-no-conflict-markers-push  rc=0  PASS, 5 files scanned, 0 markers
- check-tree-size-push            rc=0  PASS, selftest 24/24, 0 structural faults
- check-no-revert-push            rc=0  PASS, selftest 3/3, 5 files, 0 reverts
- check-hook-installation         rc=0  PASS, 12 checks, wiring intact
- check-test-tree-divergence-delta rc=0 PASS — 3213 pre-existing offender(s),
  0 introduced by this range

Divergence-delta step-over record (required by .claude/rules/vcs.md when
landing on a delta-PASS). The pre-existing red is IDENTICAL at base and tip.
Base verdict at origin/main: `FAIL — 3941 diverged vs 965 baselined (3079 new,
103 fixed-but-still-baselined); 32 mirror-only (31 unallowlisted, 0
stale-allowlist)`. Offender list (3,941 lines) saved by the helper to
`$TMPDIR/test_tree_divergence_preexisting.txt`. The only mirror-pair paths this
range touches are the two copies of the new spec, added byte-identical:
  test/01_unit/browser_engine/web_selector_parsed_equivalence_spec.spl
  test/unit/browser_engine/web_selector_parsed_equivalence_spec.spl

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TVraTPgGDVypESTsVqXPgi
@ormastes
ormastes merged commit 82fd736 into main Sep 13, 2026
1 of 35 checks passed
@ormastes
ormastes deleted the work/web-perf-round9-2026-09-13 branch September 13, 2026 12:37

This branch was successfully deployed

No deployments
self-review-admission — 177ee739 Deployed Sep 13, 2026 by ormastes via Invalidate changed PR self-review state #5002
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant